package edu.hzit.data.controller;

import edu.hzit.data.entity.User;
import edu.hzit.data.service.UserService;
import edu.hzit.data.utils.DbOperate;
import edu.hzit.data.utils.ResultVO;
import edu.hzit.data.utils.TokenUserPair;
import edu.hzit.data.utils.TokenUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("user")
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    private DbOperate dbOperate;
    @Resource
    private HttpSession session = null;

    @RequestMapping("login")
    private ResultVO login(String phone, String password){
        try {
            User user  = userService.login(phone, password);
            if (user!=null){
                //登录成功
                //request.getSession().setAttribute("user",user);
                //b.生成token（自定义生成规则）
                String token = TokenUtil.createToken(user.getUserName());
                //将token和user对象封装到tokenUserPair里面
                TokenUserPair tokenUserPair = new TokenUserPair(token, user);
                //c.将token回传给前端
                return new ResultVO(1,"登陆成功",tokenUserPair);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResultVO(0,"fail");
    }

    @RequestMapping("insert")
    private int insert(User user){
//         System.out.println(user);
        return userService.insert(user);
    }

    @RequestMapping("loadList")
    private List<User> loadList(){
        return userService.loadList();
    }

    @RequestMapping("selectById")
    private User selectById(int id){
        return userService.selectById(id);
    }

//    @RequestMapping("selectByPhone")
//    private User selectByPhone(String phone){
//        return userService.selectByPhone(phone);
//    }

    @RequestMapping("delete")
    private int delete(int id){
        return userService.delete(id);
    }

    @RequestMapping("update")
    private int update(User user){
        return userService.update(user);
    }

    @RequestMapping("updPwd")
    private int updPwd(User user){
        return userService.updPwd(user);
    }

    @RequestMapping("updPassword")
    private int updPassword(@Param("userId") int userId,@Param("oldPwd") String oldPwd, @Param("newPwd") String newPwd){
        return userService.updPassword(userId,oldPwd, newPwd);
    }

    @RequestMapping("dbBackUp")
    private String exportDatabaseTool() throws Exception {
        String backName = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date())+".sql";
//        session.removeAttribute("name");
        session.setAttribute("name",backName);
        System.out.println(session.getAttribute("name"));
        try {
            if (dbOperate.exportDatabaseTool("127.0.0.1", "root", "123", "/root/data/DbOperate/", backName, "personal_data")) {
                System.out.println("数据库成功备份！！！");
                return backName;
            } else {
                System.out.println("数据库备份失败！！！");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping("dbRestore")
    private void dbRestore(String name) throws Exception {
        System.out.println(name);
        if (name != null) {
            // 拼接备份文件路径
            String filePath = "/root/data/DbOperate/" + name;
            // 调用恢复数据库的方法
            dbOperate.dbRestore(filePath);
        } else {
            System.out.println("未在 session 中找到备份文件名。");
        }
    }

}
